package com.infosys.cabplanning.service.selectTrip;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.support.DaoSupport;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.infosys.cabplanning.common.domain.Cab;
import com.infosys.cabplanning.common.domain.CriteriaDTO;
import com.infosys.cabplanning.common.domain.Driver;
import com.infosys.cabplanning.common.domain.Schedule;
import com.infosys.cabplanning.common.domain.SelectTrip;
import com.infosys.cabplanning.common.domain.TripDetails;
import com.infosys.cabplanning.helper.JsonFormatter;
import com.infosys.cabplanningdao.CabPlanningDAOService;

@Controller
public class SelectTripController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @Autowired
    public CabPlanningDAOService daoService;
    
    @Autowired
    JsonFormatter jsonFormatter;
    
    @RequestMapping(value="selectTrip", method = RequestMethod.POST,produces="application/json",consumes="application/json")
    public @ResponseBody List<SelectTrip> selectTrip(@RequestBody String schId) throws Exception {
    		System.out.println("Entered SelectTrip method");
    		CriteriaDTO criteriaDTO = new CriteriaDTO();
    		criteriaDTO.setSchedule(new Schedule());
    		criteriaDTO.getSchedule().setScheduleId(Integer.parseInt(schId));
    		List<TripDetails> tripDetails = daoService.getTripDetails(criteriaDTO);
    		List<SelectTrip> selectTripList = getSelectTripList(tripDetails);
    		return selectTripList;
    }
    
    private List<SelectTrip> getSelectTripList(List<TripDetails> tripDetailsList) {
    	List<SelectTrip> selectTripList = new ArrayList<SelectTrip>();
    	if (!tripDetailsList.isEmpty()){
    		for(TripDetails tripDetails : tripDetailsList) {
    			selectTripList.add(convertToSelectTripObj(tripDetails));
    		}
    	}
    	return selectTripList;
	}

	private SelectTrip convertToSelectTripObj(TripDetails tripDetails) {
		SelectTrip selectTrip = new SelectTrip();
		selectTrip.setCabId(tripDetails.getCab().getCabId());
		selectTrip.setDriverId(tripDetails.getDriver().getDriverId());
		selectTrip.setScheduleId(tripDetails.getTripDetailsId());
		return selectTrip;
	}

	@RequestMapping(value="saveselectTrip", method = RequestMethod.POST ,produces="application/json",consumes="application/json")
    public @ResponseBody List<SelectTrip> saveselectTrip(@RequestBody String json) throws Exception {
    		System.out.println("Entered saveselectTrip method");
    		System.out.println("Request : "+json);
    		List<Object> list = jsonFormatter.convertToList(json, new SelectTrip());
    		System.out.println("Obj List length : "+list.size());
    		List<SelectTrip> selectTripList = new ArrayList<SelectTrip>();
    		System.out.println("Ck 1");
    		for(Object obj : list) {
    			selectTripList.add((SelectTrip)obj);
    		}
    		System.out.println("List length : "+selectTripList.size());
    		System.out.println("Trip Select : "+selectTripList.get(0).getCabId() + " "+selectTripList.get(0).getDriverId());
    		List<TripDetails> tripDetailsList = getTripDetailsList(selectTripList);
    		daoService.saveTripDetails(tripDetailsList);
    		return selectTripList;
    }
    
    private List<TripDetails> getTripDetailsList(List<SelectTrip> selectTripList) {
    	List<TripDetails> tripDetailsList = new ArrayList<TripDetails>();
    	if (!selectTripList.isEmpty()) {
    		for(SelectTrip selectTrip : selectTripList) {
    			tripDetailsList.add(convertToTripDetails(selectTrip));
    		}
    	}
    	return tripDetailsList;
	}

	private TripDetails convertToTripDetails(SelectTrip selectTrip) {
		TripDetails tripDetails = new TripDetails();
		tripDetails.setCab(new Cab());
		tripDetails.setSchedule(new Schedule());
		tripDetails.setDriver(new Driver());
		tripDetails.getCab().setCabId(selectTrip.getCabId());
		tripDetails.getSchedule().setScheduleId(selectTrip.getScheduleId());
		tripDetails.getDriver().setDriverId(selectTrip.getDriverId());
		return tripDetails;
	}

	@RequestMapping(value="selectTripSchedule", method = RequestMethod.POST ,produces="application/json",consumes="application/json")
    public @ResponseBody List<Schedule> selectTripSchedule(@RequestBody String json) throws Exception {
    		System.out.println("Entered Drivers method");
    		Schedule scheduleSearchDTO = new Schedule();
    		scheduleSearchDTO.setDate(new GregorianCalendar());
    		return daoService.findSchedules(scheduleSearchDTO);
    }
    
}